var config = require("config");
cc.Class({
    extends: cc.Component,

    properties: {
        xMinSpeed: {
            default:0,
            type:cc.Integer,
            tooltip: 'x轴最小速度',
        },
        xMaxSpeed: {
            default:0,
            type:cc.Integer,
            tooltip: 'x轴最大速度',
        },
        yMinSpeed: {
            default:0,
            type:cc.Integer,
            tooltip: 'y轴最小速度',
        },
        yMaxSpeed: { 
            default:0,
            type:cc.Integer,
            tooltip: 'y轴最大速度',
        },
        initHP: {
            default:0,
            type:cc.Integer,
            tooltip: '初始生命值',
        }, 
        initSpriteFrame:{
            default: null,
            type: cc.SpriteFrame,
            tooltip: '初始化的图像'
        }, 
        score: {
            default:0,
            type:cc.Integer,
            tooltip: '死后获得的分数'
        },
        sprArray :{
            default:[],
            type:[cc.SpriteFrame],
        },
        spr2Array :{
            default:[],
            type:[cc.SpriteFrame],
        },
        enemyType:{
            default:0,
            type:cc.Integer,
            tooltip: '怪物类型'
        },
        progress_hp: {
            default: null,
            type: cc.ProgressBar
        },
        lab_info:cc.Label,
    },

    // use this for initialization
    onLoad: function () {
        cc.director.getCollisionManager().enabled=true;
        this.bossGroup = this.node.parent.getComponent('bossGroup');
        this.bossBruise = false;//boss残血
        this.initHP = config.BossMaxHp;

        this.lab_info.string = "";
    },
    init: function(){
        if (this.node.group != 'boss'){
            this.node.group = 'boss';
        }
        this.bossBruise = false;
        this.initHP = config.BossMaxHp;
        if (this.hP != this.initHP){
            this.hP = this.initHP;
            this.setEnemyHp(this.hP);
            this.progress_hp.node.active = false;
        }
        var nSprite = this.node.getComponent(cc.Sprite);
        this.sprIndex = Math.floor(Math.random()*4);
        nSprite.spriteFrame = this.sprArray[this.sprIndex];

        // this.lab_info.string = "攻击"+config.BossBulletHpDrop+"血量"+this.hP; 
    },
    setEnemyHp: function(hp){
        this.progress_hp.node.active = true;
        let _hp = hp/this.initHP;
        this.progress_hp.progress = _hp;
    },
    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        if (this.bossGroup.eState != D.commonInfo.gameState.start){
            return ;
        }
    },
    //碰撞检测
    onCollisionEnter: function(other,self){

        if(other.node.group == 'hero'){
        }
        
        if (other.node.group != 'bullet'){
            return ;
        }
        var bullet = other.node.getComponent('bullet');

        // console.log("bullet ",bullet)

        if (this.hP>0){//防止再次碰撞
            if(bullet){
                this.hP -= bullet.hpDrop;
            }
            this.setEnemyHp(this.hP);

            if(!this.bossBruise&&this.hP<this.initHP/2){
                this.bossBruise = true;
                var nSprite = this.node.getComponent(cc.Sprite);
                nSprite.spriteFrame = this.spr2Array[this.sprIndex];
            }

        } else {
            return ;
        }
        if (this.hP <= 0){
            this.node.group = 'default'; //不让动画在执行碰撞
            //播放动画
            var anim = this.getComponent(cc.Animation);
            // var animName = self.node.name + '_ani';
            var animName = 'enemy_ani';
            anim.play(animName);
            anim.on('finished',  this.onFinished, this);
            this.progress_hp.node.active = false;

            //掉落道具
            for(var i=0;i<10;i++){
                var disx =  Math.floor(Math.random()*300);
                var disy =  Math.floor(Math.random()*150);
                if(i%2==0){
                    disx = -disx;
                }
                var newV2 = cc.v2(this.node.x+disx,this.node.y+disy);
                this.bossGroup.initProp(newV2);
            }
            
        }
    },
    //动画结束后 动画节点回收
    onFinished: function(event) { 
        this.bossGroup.enemyDied(this.node, this.score);
        this.bossGroup.main.bossDie();
    },
    //播放死亡动画
    playDieAni(){
        this.hP = 0;
        this.node.group = 'default'; //不让动画在执行碰撞
        var anim = this.getComponent(cc.Animation);
        var animName = 'enemy_ani';
        anim.play(animName);
        anim.on('finished',  this.onFinished, this);
        this.progress_hp.node.active = false;

        //掉落道具
        var newV2 = cc.v2(this.node.x,this.node.y);
        this.bossGroup.initProp(newV2);
    },
    getBossPos(){
        return this.node.getPosition();
    },
});
